package laba_1_3;

import java.util.regex.*;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

//VS4E -- DO NOT REMOVE THIS LINE!
public class mWindow extends JFrame {
	CalcImpl C = new CalcImpl();
	private static final long serialVersionUID = 1L;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JTextField jTextField0;
	private JTextField jTextField1;
	private JTextField jTextField3;
	private JTextField jTextField2;
	private JLabel jLabel3;
	private JLabel jLabel0;
	private JButton jButton0;
	private JButton jButton1;
	private JTextField jTextField5;
	private JTextField jTextField4;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public mWindow() {
		initComponents();
	}

	private void initComponents() {
		setTitle("Calculator");
		setLayout(new GroupLayout());
		add(getJLabel1(), new Constraints(new Leading(148, -27, 10, 10), new Leading(68, 10, 12, 12)));
		add(getJLabel2(), new Constraints(new Leading(127, -16, 12, 12), new Leading(70, 12, 12)));
		add(getJTextField0(), new Constraints(new Leading(23, 112, 10, 10), new Leading(18, 12, 12)));
		add(getJTextField1(), new Constraints(new Leading(23, 112, 12, 12), new Leading(60, 12, 12)));
		add(getJTextField3(), new Constraints(new Leading(161, 112, 10, 10), new Leading(18, 12, 12)));
		add(getJTextField2(), new Constraints(new Leading(161, 112, 12, 12), new Leading(60, 12, 12)));
		add(getJLabel3(), new Constraints(new Leading(144, 28, 10, 10), new Leading(60, 24, 12, 12)));
		add(getJLabel0(), new Constraints(new Leading(144, 24, 12, 12), new Leading(18, 12, 12)));
		add(getJButton0(), new Constraints(new Leading(283, 42, 10, 10), new Leading(18, 20, 12, 12)));
		add(getJButton1(), new Constraints(new Leading(283, 12, 12), new Leading(60, 20, 12, 12)));
		add(getJTextField5(), new Constraints(new Trailing(12, 112, 336, 337), new Leading(18, 12, 12)));
		add(getJTextField4(), new Constraints(new Trailing(12, 112, 336, 336), new Leading(62, 12, 12)));
		setSize(458, 108);
	}

	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
		}
		return jTextField4;
	}

	private JTextField getJTextField5() {
		if (jTextField5 == null) {
			jTextField5 = new JTextField();
		}
		return jTextField5;
	}

	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("=");
			jButton1.addMouseListener(new MouseAdapter() {
	
				public void mouseClicked(MouseEvent event) {
					jButton1MouseMouseClicked(event);
				}
			});
		}
		return jButton1;
	}

	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText("=");
			jButton0.addMouseListener(new MouseAdapter() {
	
				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("-");
		}
		return jLabel0;
	}

	private JLabel getJLabel3() {
		if (jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("*");
		}
		return jLabel3;
	}

	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
		}
		return jTextField2;
	}

	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
		}
		return jTextField3;
	}

	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
		}
		return jTextField1;
	}

	private JTextField getJTextField0() {
		if (jTextField0 == null) {
			jTextField0 = new JTextField();
		}
		return jTextField0;
	}

	private JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("*");
		}
		return jLabel2;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("*");
		}
		return jLabel1;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				mWindow frame = new mWindow();
				frame.setDefaultCloseOperation(mWindow.EXIT_ON_CLOSE);
				frame.setTitle("mWindow");
				frame.getContentPane().setPreferredSize(frame.getSize());
				frame.pack();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
			}
		});
	}

	private void jButton0MouseMouseClicked(MouseEvent event) {
		Pattern p1 = Pattern.compile("[01]+");
		Matcher m1, m2;
		m1 = p1.matcher(jTextField0.getText());
		m2 = p1.matcher(jTextField3.getText());
		if(m1.matches() && m2.matches()) {
			jTextField5.setText(C.binarySubtraction(jTextField0.getText(), jTextField3.getText()));
		}
		else {
			jTextField5.setText("Wrong input!");
		}
	}

	private void jButton1MouseMouseClicked(MouseEvent event) {
		Pattern p2 = Pattern.compile("[012]+");
		Matcher m3,m4;
		m3 = p2.matcher(jTextField1.getText());
		m4 = p2.matcher(jTextField2.getText());
		if(m3.matches() && m4.matches()) {
			jTextField4.setText(C.tetraMult(jTextField1.getText(), jTextField2.getText()));
		}
		else {
			jTextField4.setText("Wrong input!");
		}
	}

}
